<?php

/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/4/17
 * Time: 19:36.
 */

namespace App\Http\Controllers\Bowen;

use App\Http\Classes\Bowen;
use App\Http\Classes\BowenOriginal;
use App\Http\Classes\Figure;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Http\Classes\Log;

class BowenController extends Controller
{
    public function sBowen(Request $request)
    {
        if ($request->has('bowen_id')) {
            try {
                $bowen_id = $request->input('bowen_id');
                $bowen = new Bowen($bowen_id);
                $data = $bowen->getBowen();
                if (null != $data) {
                    return $this->response(200, '获取成功', $data);
                } else {
                    return $this->response(304, '没有数据', $data);
                }
            } catch (\Exception $exception) {
                Log::userError(__FUNCTION__, $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);

                return $this->response(500, '系统内部错误！'.$exception->getMessage(), null);
            }
        } else {
            return $this->response(503, '缺少必要参数！', null);
        }
    }

    public function getAllBowen()
    {
        try {
            $cache = Redis::get('all_bowens');
            if (false) {
                $cache = json_decode($cache);

                return $this->response(200, '获取成功', ['bowens' => $cache]);
            } else {
                $data = Bowen::getAllBowen();
                if (null != $data) {
                    Redis::set('all_bowens', json_encode($data));

                    return $this->response(200, '获取成功', ['bowens' => $data]);
                } else {
                    return $this->response(304, '没有数据', $data);
                }
            }
        } catch (\Exception $exception) {
            Log::userError(__FUNCTION__, $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);

            return $this->response(500, '系统内部错误！'.$exception->getMessage(), null);
        }
    }

    public function pubBowen(Request $request)
    {
        try {
            if ($request->has('bowen_content')) {
                $dataArray = [];
                $dataArray['bowen_content'] = $request->input('bowen_content');
                $dataArray['bowen_user_id'] = $_COOKIE['user_id'];
                $dataArray['bowen_img_count'] = $request->input('bowen_img_count');
                if ($dataArray['bowen_img_count'] > 0 && $dataArray['bowen_img_count'] <= 9) {
                    $urls = new Figure($request->input('bowen_img_srcs'));
                    $urls = $urls->getResult();
                    //var_dump($urls);
                    $dataArray['bowen_img_urls'] = json_encode($urls['urls']);
                    //var_dump($dataArray['bowen_img_urls']);
                } else {
                    $dataArray['bowen_img_urls'] = null;
                }
                //var_dump($dataArray['bowen_img_urls']);
                if ('' != $dataArray['bowen_content']) {
                    $bowen = Bowen::pubBowen($dataArray);
                    if ($bowen) {
                        return $this->response(200, '发表成功！', $bowen);
                    } else {
                        return $this->response(304, '发表失败！', null);
                    }
                } else {
                    return $this->response(304, '发送内容不能为空', null);
                }
            } else {
                return $this->response(503, '缺少必要参数！', null);
            }
        } catch (\Exception $exception) {
            Log::userError(__FUNCTION__, $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);

            return $this->response(500, '系统内部错误！'.$exception->getMessage(), null);
        }
    }

    public function likeBowen(Request $request)
    {
        try {
            if ($request->has('bowen_id')) {
                $bowen_id = $request->input('bowen_id');
                $bowen = new Bowen($bowen_id);
                $data = $bowen->likeBowen();
                if ($data) {
                    return $this->response(200, '点赞成功！', $data);
                } else {
                    return $this->response(304, '点赞失败！', $data);
                }
            } else {
                return $this->response(503, '缺少必要参数！', null);
            }
        } catch (\Exception $exception) {
            Log::userError(__FUNCTION__, $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);

            return $this->response(500, '系统内部错误！'.$exception->getMessage(), null);
        }
    }

    public function getUserBowen(Request $request)
    {
        try {
            $dataArray = $request->all();
            $data = Bowen::getUserBowen($dataArray);
            if ($data) {
                return $this->response(200, '获取成功！', $data);
            } else {
                return $this->response(304, '获取失败！', $data);
            }
        } catch (\Exception $exception) {
            Log::userError(__FUNCTION__, $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);

            return $this->response(500, '系统内部错误！'.$exception->getMessage(), null);
        }
    }

    public function bowenPicUpload()
    {
        try {
            $original = new BowenOriginal($_FILES['pic']);
            $data = $original->getResult();
            $msg = $original->getMsg();
            $code = $original->getCode();

            return $this->response($code, $msg, $data);
        } catch (\Exception $exception) {
            Log::userError(__FUNCTION__, $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);

            return $this->response(500, '系统内部错误！'.$exception->getMessage(), null);
        }
    }

    public function repost(Request $request)
    {
        try {
            $data = [];
            $data['repost_id'] = $request->input('repost_id');
            $data['bowen_content'] = $request->input('repost_content');
            $data['bowen_user_id'] = $_COOKIE['user_id'];
            if ('' != $data['bowen_content']) {
                $data = Bowen::repost($data);
                //var_dump($data);
                if (false === $data) {
                    return $this->response(304, '转发失败！', false);
                } elseif (null === $data) {
                    return $this->response(304, '转发成功！获取失败', null);
                } else {
                    return $this->response(200, '转发获取成功！', $data);
                }
            } else {
                return $this->response(503, '缺少必要参数！', null);
            }
        } catch (\Exception $exception) {
            Log::userError(__FUNCTION__, $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);

            return $this->response(500, '系统内部错误！'.$exception->getMessage(), null);
        }
    }

    private function response($status, $message, $data)
    {
        $response = [];
        $response['status'] = $status;
        $response['message'] = $message;
        $response['data'] = $data;

        return response()->json($response);
    }
}
